package cn.cloud9.designpattern.dp05proxy.proxy_02_cglib;

import cn.cloud9.designpattern.dp05proxy.proxy_02_cglib.po.Student;
import cn.cloud9.designpattern.dp05proxy.proxy_01_static.Personal;

import java.time.LocalDateTime;

/**
 * @projectName: Java
 * @author: 戴知舟
 * @date: 2022年05月23日 13:57
 * @version: 1.0
 */
public class CglibProxyTest {
    public static void main(String[] args) {

        // CGLIB代理
        CglibProxyInterceptor interceptor = new CglibProxyInterceptor();

        Personal personal = interceptor.getInstance(Personal.class);

        personal.rentHouse();


        // 使用匿名内部类的方式代理
        personal = new Personal() {

            private void beforeRent() {
                System.out.println("租房前准备手续");
            }

            @Override
            public void rentHouse() {
                this.beforeRent();
                System.out.println("个人租房！ + 代理来完成！");
                this.afterRent();
            }

            private void afterRent() {
                System.out.println("租房后交互尾款");
            }
        };
        personal.rentHouse();


        // 对Po进行临时字段扩展
        Student student = new Student(1001, "阿伟", LocalDateTime.now()) {
            // 追加一个扩展字段
            private Boolean gender;

            public Boolean getGender() {
                return gender;
            }

            public void setId(Integer id, Boolean gender) {
                this.id = id;
                this.gender = gender;
            }

            public void setGender(Boolean gender) {
                this.gender = gender;
            }

            {
                this.setGender(true);
            }

            @Override
            public String toString() {
                return super.toString() + " $classname{" + "gender=" + gender + '}';
            }
        };
        System.out.println(student);
        System.out.println(student.getClass().getName());

    }
}
